package com.spring.boot;

import com.spring.context.ConfigurableApplicationContext;
import com.spring.context.annotation.AnnotationConfigApplicationContext;
import com.spring.core.io.support.SpringFactoriesLoader;

/**
 * @author liyuncong
 * @version 1.0
 * @file ApplicationContextFactory
 * @brief ApplicationContextFactory
 * @details ApplicationContextFactory
 * @date 2023-01-13
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                        NAME               DESCRIPTION
 * 2023-01-13                  liyuncong          Created
 */
public interface ApplicationContextFactory {

    ApplicationContextFactory DEFAULT = webApplicationType -> {
        try {
            for (ApplicationContextFactory candidate : SpringFactoriesLoader.loadFactories(
                ApplicationContextFactory.class,
                ApplicationContextFactory.class.getClassLoader())) {
                ConfigurableApplicationContext context = candidate.create(webApplicationType);
                if (context != null) {
                    return context;
                }
            }
            return new AnnotationConfigApplicationContext();
        } catch (Exception exception) {
            throw new IllegalStateException("Unable create a default ApplicationContext instance, "
                + "you may need custom ApplicationContextFactory", exception);
        }
    };

    ConfigurableApplicationContext create(WebApplicationType webApplicationType);
}
